<?php
/**
 * V6
 *
 */
class data_invitation_weibo extends data_invitation {
	public function __construct() {
		parent::__construct ();
		$this->expire_days = 6; // public code的过期时间，天数
	}
	/**
	 * 列出可以被邀请的微博好友
	 * 排除已经是成员的
	 * 排除已经发送过邀请的
	 *
	 * @param int $uid        	
	 * @param int $circle_id        	
	 * @return array = array (
	 *         array (
	 *         id			=> // weibo_id
	 *         screen_name	=> //
	 *         ),
	 *         ...............
	 *         )
	 */
	public function friend_for_invite($uid, $circle_id) {
		$uid = intval ( $uid );
		$circle_id = intval ( $circle_id );
		
		if ($uid and $circle_id) {
			$data_user = new data_user ();
			$user = $data_user->one ( $uid );
			$data_circle = new data_circle ();
			// 获得user的weibo id 和 circle的权限
			if ($user ['weibo_id'] and $data_circle->is_member ( $uid, $circle_id )) {
				// 获取user的微博好友的微博id和screen name
				$data_weibo = new data_weibo ();
				$weibo_friends = $data_weibo->bilateral ( $user ['weibo_id'] ); // 返回
				                                                            // array
				                                                            // (array(id,
				                                                            // screen_name),
				                                                            // ...)
				if (is_array ( $weibo_friends )) {
					// 获取圈子member的微博id
					$member = $data_circle->list_member ( $uid, $circle_id, 1, 0 ); // 返回
					                                                             // array
					                                                             // (array(id,
					                                                             // circle_id,
					                                                             // user_id,
					                                                             // is_admin,
					                                                             // nickname,
					                                                             // email_id,
					                                                             // weibo_id,
					                                                             // friend_nickname),
					                                                             // ...)
					if (is_array ( $member )) {
						foreach ( $member as $v ) {
							if ($v ['weibo_id']) {
								$m [] = $v ['weibo_id'];
							}
						}
					}
					// 获取已经发送过邀请的accepter
					$member = $this->db->execute ( '
                                                select
                                                        accepter_id
                                                from
                                                        invitation_weibo
                                                where
                                                        inviter_id	= "' . $uid . '"		and
                                                        circle_id	= "' . $circle_id . '"
                                        ' );
					if (is_array ( $member )) {
						foreach ( $member as $v ) {
							$m [] = $v ['accepter_id'];
						}
						unset ( $member );
					}
					// 遍历weibo friends，排除圈子成员和已经发送过邀请的
					if (is_array ( $m )) {
						foreach ( $weibo_friends as $v ) {
							if (array_search ( $v ['id'], $m ) === false) {
								$weibo_friends_m [] = $v;
							}
						}
						if (is_array ( $weibo_friends_m )) {
							return $weibo_friends_m; // 返回排除之后的互粉列表
						} else
							return false;
					} else
						return $weibo_friends; // 没有需要排除的，直接返回互粉列表即可
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}
	/**
	 * 发布微博@给被邀请的好友
	 * 发布成功后将邀请记录到数据库中
	 *
	 * @param int $uid        	
	 * @param int $circle_id        	
	 * @param String $accepter_weibo_id_str        	
	 * @return bool // 1: 成功; 0:参数错误；-1:token过期；-2:发布微博错误；-3:超过140字了
	 */
	public function send($uid, $circle_id, $accepter_weibo_id_str) {
		$my_friends = $this->friend_for_invite ( $uid, $circle_id ); // 可以被邀请的微博用户，已经检查过权限了，为了核实参数id_str
		$accepter_weibo_id_array = format_id_string ( $accepter_weibo_id_str, true ); // 返回array
		                                                                           // 检查circle权限
		if (is_array ( $my_friends ) and is_array ( $accepter_weibo_id_array )) {
			// 循环核实weibo id
			foreach ( $my_friends as $v ) {
				if (array_search ( $v ['id'], $accepter_weibo_id_array ) !== false) {
					$real_friends [] = $v; // 核实并重构
				}
			}
			unset ( $my_friends );
			unset ( $accepter_weibo_id_array );
			// 核实weibo id完成
			if (is_array ( $real_friends )) {
				foreach ( $real_friends as $v ) {
					$at .= '@' . $v ['screen_name']; // 构造微博中“@”的字符串
					$ids .= $v ['id'] . ','; // 构造ids给add函数
				}
				// 发布一条微博，并@给被邀请者
				$data_weibo = new data_weibo ();
				$data_circle = new data_circle ();
				$circle = $data_circle->one ( $uid, $circle_id ); // 用来判断privacy
				if ($circle ['privacy'] == 'private') {
					$msg = '我创建了一个主题阅读圈，邀请您加入：';
				} else {
					$msg = '我创建了一个主题阅读圈《' . $circle ['title'] . '》，邀请您加入：';
				}
				$code = $this->make_code ( 'weibo', $uid, $circle_id );
				$url_short = $data_weibo->short_url_shorten ( 'http://uread.me/invitation.php?ureadmecode=' . $code );
				$msg .= $url_short . ' ' . $at;
				$weibo_update = $data_weibo->update ( $msg ); // 发布一条微博 // 1: 成功;
				                                           // 0:参数错误；-1:token过期；-2:发布微博错误；-3:超过140字了
				if ($weibo_update > 0) {
					$this->add ( $uid, $circle_id, $ids, $code ); // 写入数据库
				}
				return $weibo_update; // // 1: 成功;
				                      // 0:参数错误；-1:token过期；-2:发布微博错误；-3:超过140字了
			} else
				return false;
		} else
			return false;
	}
	
	/**
	 * 通过邀请码返回邀请者的id和圈子id
	 * 可能会返回多条记录
	 *
	 * @param String $code        	
	 * @return Array
	 */
	public function decode($code) {
		if ($this->is_code ( $code ) == 'weibo') {
			$execute = $this->db->execute ( '
                                select
                                        *
                                from
                                        invitation_weibo
                                where
                                        code 			= "' . $code . '"	and
                                        is_accepted		= 0
                        ' );
			if (is_array ( $execute )) {
				// 检查权限
				$data_circle = new data_circle ();
				if ($data_circle->is_member ( $execute [0] ['inviter_id'], $execute [0] ['circle_id'] )) {
					return $execute;
				} else {
					// 邀请者已经不再是圈子成员了，删除记录
					$this->db->execute ( '
                                                delete from
                                                        invitation_weibo
                                                where
                                                        code = "' . $code . '"
                                        ' );
					return false;
				}
			} else
				return false;
		} else
			return false;
	}
	public function accept($decode, $weibo_id, $uid) {
		$uid = intval ( $uid );
		$inv = $this->is_in_decode ( $weibo_id, $decode ); // 返回invitation_weibo表中对应accepter_id
		                                                // == weibo_id 的记录
		
		if ($uid and $inv) {
			// 发布Event：加入圈子、加为好友，发布notice
			$data_event = new data_event ();
			$event_param = array (
					'user_id' => $uid,
					'inviter_id' => $inv ['inviter_id'],
					'circle_id' => $inv ['circle_id'] 
			);
			$data_event->add ( 'invitation_accept', $event_param, 'high' );
			
			// 标记invitation记录为已经接受
			
			$this->update_is_accepted ( $inv ['id'], 1 );
			
			return $uid;
		} else
			return false;
	}
	private function add($inviter_id, $circle_id, $accepter_ids, $code) {
		$accepter_id_array = format_id_string ( $accepter_ids, true );
		if ($inviter_id and $circle_id and is_array ( $accepter_id_array ) and $code) {
			$sql = '
                                insert into
                                        invitation_weibo
                                (inviter_id, circle_id, accepter_id, code, dt, is_accepted)
                                        values
                        ';
			$dt = date ( 'Y-m-d H:i:s' );
			foreach ( $accepter_id_array as $aid ) {
				$sql .= '("' . $inviter_id . '", "' . $circle_id . '", "' . $aid . '", "' . $code . '", "' . $dt . '", 0),';
			}
			$sql = substr ( $sql, 0, - 1 );
			return $this->db->execute ( $sql );
		} else
			return false;
	}
	private function is_in_decode($weibo_id, $decode) {
		if (is_array ( $decode ) and data_user::is_weibo_id ( $weibo_id )) {
			foreach ( $decode as $v ) {
				if ($v ['accepter_id'] == $weibo_id) {
					return $v;
				}
			}
			return false;
		} else
			return false;
	}
	private function update_is_accepted($id, $is_accepted) {
		$id = intval ( $id );
		$is_accepted = intval ( $is_accepted );
		
		if ($id) {
			return $this->db->execute ( '
                                update
                                        invitation_weibo
                                set
                                        is_accepted		= "' . $is_accepted . '"
                                where
                                        id	= "' . $id . '"
                        ' );
		} else
			return false;
	}
}
?>